package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.event.PageButtonEvent;
import ru.spb.osll.myfit.client.event.PageButtonEvent.PageButtonEventType;
import ru.spb.osll.myfit.client.widget.IStyleConstants;
import ru.spb.osll.myfit.client.widget.PageButton;
import ru.spb.osll.myfit.client.widget.PageButtonEventListener;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;

public abstract class AbstractView implements IView, PageButtonEventListener{
  private Widget m_contentPanel;
  private ScrollPanel m_scrollPanel;

  public Widget getRootWidget() {
    return createRootPanel();
  }

  protected Widget createRootPanel() {
    VerticalPanel rootPanel = new VerticalPanel();
    rootPanel.setStyleName("root-panel");
    rootPanel.add(createHeader());

    Widget editPanel = createEditPanel();
    m_contentPanel = createContentPanel();
    
    m_scrollPanel = new ScrollPanel(m_contentPanel);
    m_scrollPanel.setStyleName("scrollableContainer");
    DOM.setStyleAttribute(m_scrollPanel.getElement(), "position", "absolute");

    rootPanel.add(editPanel);
    rootPanel.add(m_scrollPanel);
  return rootPanel;
  }

  private Widget createHeader(){
    HorizontalPanel panel = new HorizontalPanel();
    panel.setStyleName(IStyleConstants.HEADER_PANEL_STYLE_NAME);
    
    Label myFitLabel = new Label();
    myFitLabel.setStyleName(IStyleConstants.HEADER_LABEL_STYLE_NAME);
    
    panel.add(myFitLabel);
    panel.setCellHorizontalAlignment(myFitLabel, HasHorizontalAlignment.ALIGN_LEFT);
    
    HorizontalPanel iconPanel = new HorizontalPanel();
    iconPanel.setStyleName(IStyleConstants.HEADER_ICON_PANEL_STYLE_NAME);
    PageButton home = new PageButton(IStyleConstants.HOME_BUTTON_STYLE_NAME, PageButtonEventType.HOME, this); 
    PageButton testResults = new PageButton(IStyleConstants.RESULTS_BUTTON_STYLE_NAME, PageButtonEventType.LIST_OF_EXECUTING_TEST, this); 
    iconPanel.add(home);
    iconPanel.add(testResults);
    
    panel.add(iconPanel);
    panel.setCellHorizontalAlignment(iconPanel, HasHorizontalAlignment.ALIGN_RIGHT);
    
    return panel;
  }
  
  @Override
  public void actionPerformed(PageButtonEvent event) {
    switch (event.getType()) {
    case LIST_OF_EXECUTING_TEST:
      Window.Location.assign(ViewConstants.TEST_TABLE_ADDRESS);
      break;
    case HOME:
      Window.Location.assign("/");
      break;
    default:
      break;
    }
  }
  
  protected abstract Widget createContentPanel();

  protected abstract Widget createEditPanel();
}
